package com.skycong.hello77.common.baseadpater;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;

import com.skycong.hello77.R;
import com.skycong.hello77.activity.mydialogcar.MyProgressDialog_car;
import com.skycong.hello77.common.StringUtil;

/**
 * 基础的BaseAdapter类
 * Created by Kiki on 2018/6/19.
 */

public abstract class BaseActivity extends Activity implements View.OnClickListener {
    public BaseView baseView;
    private MyProgressDialog_car myProgressDialog_car;//网络请求的弹框
    private int callBackType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,竖屏
         * ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,横屏
         */
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置手机屏幕的方向
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉带默认的标题栏

        getWindow().setBackgroundDrawable(null);//解决过度绘制导致的掉帧，也就是卡顿的情况
        //ExitApplication.getInstance().addActivity(this);,退出标识，单独在建一个类进行处理
        /**
         * 设置为竖屏
         */
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    //初始化话头部标题
    public void initTitle(String title){
       baseView = new BaseView(this);
       myProgressDialog_car = new MyProgressDialog_car(this);
       baseView.setListener(this);

    }

    /**
     * 数据重新加载
     */
    public abstract void reload();

    @Override
    public void onClick(View v) {
      int id  = v.getId();
      if(id == R.id.layout_back){
          ((Activity) v.getContext()).finish();
      }else if(id == R.id.tv_nodata || id == R.id.iv_nodata){
          // TODO: 2018/6/25  先检查网络是否连接，没有连接，跳转到系统网络设置
          /*
          * Intent intent = new Intent(Settings.ACTION_SETTINGS);
          * startActivity(intent);
          * */
      }else{
          reload();
      }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(myProgressDialog_car != null){
            myProgressDialog_car.dismiss();
            myProgressDialog_car = null;
        }
        baseView = null;
    }

    /**
     * 设置加载中弹框是否显示
     * @param visible
     */
    public void setProgressDialogVisible(boolean visible){
        if(this.isFinishing()) return;
        if(myProgressDialog_car == null) return;
        if(visible && !this.isFinishing()){
            myProgressDialog_car.show();
        }else{
            myProgressDialog_car.hide();
        }
    }

    /**
     * 设置没有数据时的布局是否显示
     * @param visible
     */
    public void setNoDataViewVisible(boolean visible){
        if(this.isFinishing()) return;
        if(baseView == null || baseView.viewNoData == null)return;
        if(visible){
            baseView.viewNoData.setVisibility(View.VISIBLE);
            baseView.viewNoData.setClickable(true);
        }else {
            baseView.viewNoData.setVisibility(View.GONE);
            baseView.viewNoData.setClickable(false);
        }
    }

    /**
     * 设置加载中提示的文字
     * @param msg
     */
    public void setProgersssDialogMessage(String msg) {
        if (this.isFinishing()) return;
        if (myProgressDialog_car == null) return;
        myProgressDialog_car.setMsg(msg);
    }

    /**
     * 设置返回键的显示隐藏
     * @param visible
     */
    public void setLayoutBackVisible(int visible){
        baseView.layout_back.setVisibility(visible);
    }
    /**
     * 页面没有数据时的显示设置
     *
     * @param callBackType
     * @param text
     * @param imgResId
     */
    public void setNoDataViewShow(int callBackType, String text, int imgResId) {
        setNoDataViewShow(callBackType, text, null, imgResId, null, null);
    }

    /**
     * 页面没有数据时的显示设置
     *
     * @param callBackType
     * @param text
     * @param imgResId
     */
    public void setNoDataViewShow(int callBackType, String text,String text2, int imgResId) {
        setNoDataViewShow(callBackType, text, text2, imgResId, null, null);
    }

    /**
     * 页面没有数据时的显示设置
     *
     * @param callBackType
     * @param textRes
     * @param imgResId
     */
    public void setNoDataViewShow(int callBackType, int textRes, int imgResId) {
        String text = null;
        try {
            text = getString(textRes);
        } catch (Exception e) {
        }
        setNoDataViewShow(callBackType, text, imgResId);
    }

    /**
     * 页面没有数据时的显示设置
     *
     * @param callBackType
     * @param textRes
     * @param imgResId
     */
    public void setNoDataViewShow(int callBackType, int textRes, int textRes2, int imgResId) {
        String text = null;
        String text2 = null;
        try {
            text = getString(textRes);
            text2 = getString(textRes2);
        } catch (Exception e) {
        }
        setNoDataViewShow(callBackType, text, text2, imgResId);
    }
    /**
     * 页面没有数据时的显示设置
     *
     * @param callBackType
     * @param text
     * @param imgResId
     */
    public void setNoDataViewShow(int callBackType, String text, String text2, int imgResId, String btnText, View.OnClickListener btnOnclickListener) {
        if (this.isFinishing()) return;
        if (baseView == null || baseView.tv_nodata == null) return;
        setNoDataViewVisible(true);
        this.callBackType = callBackType;
        switch (callBackType) {
            case CallBackType.NetworkNotVaalide:
                baseView.tv_nodata.setText("网络无连接");
                baseView.tv_networknot.setVisibility(View.VISIBLE);
                baseView.tv_networknot.setText(R.string.network_set);
                break;
            case CallBackType.Success:
                baseView.tv_nodata.setText("暂无数据");
                break;
            case CallBackType.ServiceErr:
                baseView.tv_nodata.setText("网络错误");
                break;
            case CallBackType.ServiceExp:
                baseView.tv_nodata.setText("网络异常");
                break;
        }
        if (text != null && text.trim().length() != 0) {
            baseView.tv_nodata.setText(text);
        }
        if (!StringUtil.IsNull(text2)) {
            baseView.tv_networknot.setVisibility(View.VISIBLE);
            baseView.tv_networknot.setText(text2);
        }
        if (imgResId > 0&&callBackType== CallBackType.Success) {
            baseView.iv_nodata.setImageResource(imgResId);
        }else if (imgResId > 0&&callBackType!= CallBackType.Success){
            baseView.iv_nodata.setImageResource(R.mipmap.ic_no_retrun_order);
        }else if (imgResId <= 0&&callBackType== CallBackType.Success){
            baseView.iv_nodata.setImageResource(R.mipmap.ic_no_retrun_order);
        }else if (imgResId <= 0&&callBackType!= CallBackType.Success){
            baseView.iv_nodata.setImageResource(R.mipmap.ic_no_retrun_order);
        }
        if (btnText != null) {
            if (baseView.btn_nodata != null) {
//                baseViewModel.btn_nodata.setVisibility(View.VISIBLE);
                baseView.btn_nodata.setText(btnText);
            }
        } else {
            if (baseView.btn_nodata != null && callBackType != CallBackType.Success) {
//                baseViewModel.btn_nodata.setVisibility(View.VISIBLE);
                baseView.btn_nodata.setText("重新加载");
            } else if (baseView.btn_nodata != null && callBackType == CallBackType.Success) {
//                baseViewModel.btn_nodata.setVisibility(View.VISIBLE);
                baseView.btn_nodata.setText("返回首页");
            }
        }
        if (btnOnclickListener != null) {
            if (baseView.btn_nodata != null) {
//                baseViewModel.btn_nodata.setVisibility(View.VISIBLE);
                baseView.btn_nodata.setOnClickListener(btnOnclickListener);
            }
        } else {
            if (baseView.btn_nodata != null) {
//                baseViewModel.btn_nodata.setVisibility(View.VISIBLE);
                baseView.btn_nodata.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        reload();
                    }
                });
            }
        }
    }
}
